import json
from datetime import datetime, date, time
from decimal import Decimal

DATE_FORMAT = "%Y-%m-%d"
TIME_FORMAT = "%H:%M:%S"

def _json_default(obj):
    if isinstance(obj, datetime):
        # return obj.isoformat() # "2010-04-20T20:08:21.634121"
        return obj.strftime("%s %s" % (DATE_FORMAT, TIME_FORMAT))
    elif isinstance(obj, date):
        return obj.strftime(DATE_FORMAT)
    elif isinstance(obj, time):
        return obj.strftime(TIME_FORMAT)
    elif isinstance(obj, Decimal):
        return str(obj)
    raise ValueError()

def json_dumps(obj, **kwargs):
    return json.dumps(obj, default=_json_default, **kwargs)

def json_load(s):
    if(isinstance(s, str)):
        s = io.StringIO(s)
    return json.load(s)


